import {Component, ViewChild} from '@angular/core';
import {IonicPage, NavParams} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {LocationProvider} from "@providers/location";
import {MapProvider} from "@providers/map";

// declare let AMap;

@IonicPage()
@Component({
    selector: 'page-map-navigation',
    templateUrl: 'map-navigation.html',
})
export class MapNavigationPage {
    @ViewChild('map') mapElement: any;

    map: any;//地图对象
    geolocation: any;//地图定位
    navigation: any;//导航实例
    currentLocation: any;//当前位置
    mapIsComplete: boolean = false;//地图是否加载完毕
    markerLocation: any = {'lng': 114.275642, 'lat': 30.588644};//标记位置

    data: any = this.navParams.data;

    constructor(public navParams: NavParams,
                private storageService: StorageProvider,
                private location: LocationProvider, private mapService: MapProvider) {
        this.currentLocation = this.storageService.read("UserLocation");
    }

    ionViewDidLoad() {
        this.loadMap();
    }

    //加载地图
    private loadMap() {
        this.mapService.createMap(this.mapElement.nativeElement).then(map => {
            this.mapIsComplete = true;
            this.map = map;
            let marker = this.mapService.addMarker(0, this.markerLocation);
            this.map.setFitView();
            let content = `
                <div class="w08 font_size_large font_weight_bold">
                            <i class="fa fa-map-marker mr5" style="color:#347eff;"></i>${this.data.ADDRESS}
                        </div>
                        <span class="w04 mt5 fr tr">${this.data.ID}</span>
                        <span class="w08 mt5">${this.data.NAME} / ${this.data.MOBILE}</span>
                        <span class="w04 mt5 fr tr font_gray">${this.getMoneyType(parseFloat(this.data.DISTANCE) / 1000 + '')}Km</span>
                        <span class="w08 mt5 font_red">备注:${this.data.BZ ? this.data.BZ : '无'}</span>
                        <span class="w04 mt5 fr tr font_red">${this.data.THRUTIME}  ${this.data.YYSJD}</span>
            `;
            this.mapService.addInfoWindow(marker, content);
        }, err => {
            console.log(err);
        })

    }

    doNavigation() {
        this.location.amapRoute(this.markerLocation.lng, this.markerLocation.lat, this.data.ADDRESS).then();
    }

    /**
     * 对距离数据的格式化 例：4，455，455，555.45 ￥
     * @param {string} value 需要转化的元数据
     * @param {number} wlength 间隔的长度
     * @param {string} jiange 间隔的符号，例","
     * @param {number} xsleng 小数位的长度
     * @returns {string} 最终返回的个是字符
     */
    getMoneyType(value: string, wlength?: number, jiange?: string, xsleng?: number): string {
        let yshuzi = parseFloat(value);
        if (!wlength) {
            wlength = 3;
        }
        if (!jiange) {
            jiange = ',';
        }
        if (!xsleng) {
            xsleng = 1;
        }
        if (!isNaN(yshuzi)) {
            let b;
            if (yshuzi >= 0) {
                b = '';
            } else {
                b = '-';
            }
            let strxs: string = '';
            if ((value + '').indexOf(".") != -1) {
                strxs = "." + (value + '').substr((value + '').indexOf(".") + 1, xsleng);
            } else {
                strxs = ".0"
            }
            let zhshnum: number = parseInt(value, 10)//取整数，十进制
            let zhshlength: number = (Math.abs(zhshnum) + '').length;//整数位的长度
            if (zhshlength > wlength) {
                let jjj = Math.floor(zhshlength / wlength);//几个长度
                let jj = zhshlength % wlength;  //多余的
                let strz: string = Math.abs(zhshnum) + '';//
                let tr = strz.substr(0, jj);
                for (var i = 0; i < jjj; i++) {
                    tr += ',' + strz.substr(jj + i * wlength, wlength);
                }
                let ty = tr.replace(/^,/g, "");
                return (b + ty + strxs).replace(/--/g, '-');
            } else {
                return (b + zhshnum + strxs).replace(/--/g, '-');
            }
        } else {
            if (!value) {
                return "0.0"
            }
            return "不是数字格式！"
        }
    }

}
